from django.db import models
from manufacturer.models import Manufacturer, ProductModel
from idcs.models import Idc
from cabinet.models import Cabinet
from business.models import Business

STATUS = (
    (0, '线上'),
    (1, '下线'),
    (2, '故障'),
    (3, '测试'),
    (4, '发布'),
    (5, '维护')
)

# Create your models here.
class Server(models.Model):
    ip = models.GenericIPAddressField(help_text='主机管理ip', verbose_name='主机地址', db_index=True, unique=True)
    hostname = models.CharField(verbose_name='主机名', help_text='主机名', max_length=64, db_index=True, unique=True)
    cpu = models.CharField(verbose_name='CPU', help_text='CPU', max_length=64)
    mem = models.CharField(verbose_name='内存', help_text='内存', max_length=32)
    disk = models.CharField(verbose_name='磁盘', help_text='磁盘', max_length=256)
    os = models.CharField(verbose_name='OS', help_text='操作系统', max_length=64)
    sn = models.CharField(verbose_name='SN', help_text='SN编码', max_length=64)
    manufacturer = models.ForeignKey(to=Manufacturer, verbose_name='制造厂商', help_text='制造厂商')
    model_name = models.ForeignKey(to=ProductModel, verbose_name='服务器型号', help_text='服务器型号')
    # rmt_card_ip = models.GenericIPAddressField(verbose_name='管理ip', help_text='服务器管理所用的ip地址',
    # db_index=True, unique=True)
    rmt_card_ip = models.GenericIPAddressField(verbose_name='管理ip', help_text='服务器管理所用的ip地址', null=True)
    idc = models.ForeignKey(to=Idc, verbose_name='所在机房', help_text='服务器所在机房', null=True)
    cabinet = models.ForeignKey(to=Cabinet, verbose_name='机柜名称', help_text='机柜名称', null=True)
    cabinet_position = models.CharField(verbose_name='机柜内位置', help_text='机柜内位置', max_length=32, null=True)
    # uuid = models.UUIDField(verbose_name='UUID', help_text='虚拟机唯一标识', unique=True)
    business_parent = models.ForeignKey(to=Business, null=True, verbose_name="一级业务线",
                                        related_name="business_parent", help_text="一级业务线")
    business_line = models.ForeignKey(to=Business, null=True, verbose_name="二级业务线",
                                      related_name="business_line", help_text="二级业务线")
    uuid = models.CharField(verbose_name='UUID', help_text='虚拟机唯一标识', unique=True, max_length=64)
    status = models.CharField(max_length=8, verbose_name='服务器状态', null=True, help_text='服务器状态', default='维护')
    remark = models.CharField(verbose_name='备注', help_text='备注信息', max_length=128, null=True)
    last_check = models.DateTimeField(verbose_name='检测时间', help_text='检测时间', auto_now=True, db_index=True)
    ctime = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.ip

    class Meta:
        verbose_name_plural = verbose_name = '服务器信息'
        db_table = 'server'
        ordering = ['id', '-ctime']
        permissions = (
            # 添加权限，默认只有add、change、delete三项权限，额外自定义的可以在这定义
            ("view_server", "can view server"),
        )


class NetworkDevice(models.Model):
    """
    网卡
    """
    name = models.CharField(verbose_name='网卡设备名', help_text='网卡设备名', max_length=32)
    mac_address = models.CharField(verbose_name='MAC地址', help_text='网卡物理地址', max_length=20)
    host = models.ForeignKey(to=Server, verbose_name='所在服务器', help_text='网卡所在服务器')
    remark = models.CharField(verbose_name='备注', help_text='备注信息', max_length=128, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = verbose_name = '网卡信息'
        db_table = 'networkdevice'
        ordering = ['id']


class IP(models.Model):
    ip_addr = models.GenericIPAddressField(verbose_name='ip地址', help_text='ip地址', unique=True, db_index=True)
    netmask = models.CharField(verbose_name='子网掩码', help_text='子网掩码', max_length=17)
    device = models.ForeignKey(to=NetworkDevice, verbose_name='所在网卡', help_text='所在网卡')
    remark = models.CharField(verbose_name='备注', help_text='备注', max_length=128, null=True)

    def __str__(self):
        return self.ip_addr

    class Meta:
        db_table = 'ip'
        ordering = ['id']
        verbose_name_plural = verbose_name = 'IP信息'
